package cn.tedu.v5;

import java.io.*;
import java.net.*;
import java.util.*;


public class Client {
    private Socket socket;
    private String ip;
    private  static final String HOST="127.0.0.1";
    private static final Integer PORT=6666;
    private BufferedReader br;
    private PrintWriter pw;

    public Client() {
        try {
            socket=new Socket(HOST,PORT);
            br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (NullPointerException e) {
            System.out.println("友情提示: IP传入错误导致链接失败~");
        } catch (UnknownHostException e) {
            System.out.println("友情提示: IP传入错误");
        } catch (ConnectException e) {
            System.out.println("客户端链接失败: 建议检查IP或者端口号");
        } catch (IOException e) {
            System.out.println("客户端链接服务器端失败，提示: 检查IP/端口");
        }
    }
    public void start() {
        //与服务器端的交互--参考Server
        System.out.println("~~~~~~欢迎来到空桑总动员牌聊天室~~~~~~");
        ip=socket.getInetAddress().getHostAddress();
        Scanner input=new Scanner(System.in);
        try {
            //1.读入服务器端: “请输入用户名: ”
            String serverMessage=br.readLine();
            //1.1 显示"请输入用户名: “
            System.out.println(serverMessage);
            //1.2用户输入---用户名
            String username=input.nextLine();
            //1.3用户输入的用户名---写出给服务器端
            pw.println(username);
            //2.1读入服务器端发来的"请输入密码: "
            serverMessage=br.readLine();
            System.out.println(serverMessage);
            //2.2用户输入---密码--写出给服务器端
            String password=input.nextLine();
            pw.println(password);
            //3.登录之后【登录成功/登录失败】---进行聊天
            serverMessage=br.readLine(); //读入【登录成功/登录失败】
            //字符串方法: startsWith() 返回值 boolean  前缀名称一致 true
            System.out.println(serverMessage);//这里就打印"登录成功，欢迎进入聊天室"
            if (serverMessage.startsWith("登录成功")){
            //    System.out.println("登录成功，欢迎进入聊天室");
                //封一个客户端的: 读写流 ---chat聊天
                chat();
            }else {
                socket.close();
            }
        }catch (NullPointerException e){
            System.out.println("IO流传输对象，有对象为null,请立马核对。"
                    +"建议: 查看try块中对象可能赋值为null");
        } catch (IOException e){
            System.out.println("客户端断开链接!");
        }finally {
            if (br!=null) {
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭异常~");
                }
            }
            if (pw!=null)
                  pw.close();
            if (socket!=null&&!socket.isClosed()){
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭出现异常");
                }
            }
        }
    }
    //封装一个针对聊天功能: 读写 chat()
    private void chat(){
        new Thread(()->{
            try {
                String message;
                while ((message= br.readLine())!=null){
                   // System.out.println("服务器端说: "+"(输入exit退出)");
                    // 服务器端必须先输出文本才可以生效
                    System.out.println("服务器端说: "+message);
                    System.out.print("客户端请输入: "); //多余的输出提示语句
                }
        } catch (IOException e) {
                System.out.println("你的服务器端读入失败!");
            }
            }).start();

        Scanner input = new Scanner(System.in);
        while (true) {
            //这里不换行
            System.out.print("客户端请输入: ");
            String say=input.nextLine();
            pw.println("客户端说: "+say);
            //判断客户端写出为：exit 退出程序  √
            if (say.equalsIgnoreCase("exit")){
                System.out.println("服务器端即将关闭...");
                System.exit(0);
            }

        }
    }
    public static void main(String[] args) {
        Client client=new Client();
        client.start();
    }
}
